﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Video;

namespace DanMuHelper.Video
{
    [RequireComponent(typeof(VideoPlayer))]
    [RequireComponent(typeof(AudioSource))]
    public sealed class VideoPlayerMedia : BaseMediaPlayer/*<VideoPlayerMediaData>*/
    {
        public VideoPlayer Video { get; private set; }
        public AudioSource Audio { get; private set; }

        private void Awake()
        {
            Video = gameObject.GetComponent<VideoPlayer>();
            Audio = gameObject.GetComponent<AudioSource>();

            if (Audio != null)
            {
                Audio.playOnAwake = false;
                if (Audio.isPlaying)
                {
                    Audio.Stop();
                }
            }
            if (Video != null)
            {
                Video.playOnAwake = false;
                if (Video.isPlaying)
                {
                    Video.Stop();
                }
            }

        }

        protected override void PlayVideo(BaseMediaPlayerData mediaPlayerData)
        {
            if (mediaPlayerData is VideoPlayerMediaData data)
            {
                if (Video != null && data.VideoClip != null)
                {
                    Video.loopPointReached += OnVideoLoopPointReached;
                    Video.prepareCompleted += OnVideoPrepareCompleted;
                    Video.started += OnVideoStarted;
                    Video.clip = data.VideoClip;
                    Video.Play();
                }
            }
        }

        protected override void PlayAudio(BaseMediaPlayerData mediaPlayerData)
        {
            if (mediaPlayerData is VideoPlayerMediaData data)
            {
                if (Audio != null && data.AudioClip != null)
                {
                    Audio.clip = data.AudioClip;
                    Audio.Play();
                }
            }
        }

        protected override void StopVideo()
        {
            if (Video != null)
            {
                Video.Stop();
            }
        }

        protected override void StopAudio()
        {
            if (Audio != null)
            {
                Audio.Stop();
            }
        }

        protected override void StopPlayingProcess()
        {
            if (Video != null)
            {
                Video.loopPointReached -= OnVideoLoopPointReached;
                Video.prepareCompleted -= OnVideoPrepareCompleted;
                Video.started -= OnVideoStarted;
                Video.Stop();
                // FIXME: lich: ???
                if (Video.targetTexture != null)
                {
                    Video.targetTexture.Release();
                }
                if (!KeepVideoClip)
                {
                    Video.clip = null;
                }
            }

            if (Audio != null)
            {
                Audio.Stop();
            }
        }

        private void OnVideoPrepareCompleted(VideoPlayer source)
        {
            OnVideoPrepareCompleted();
        }

        private void OnVideoStarted(VideoPlayer source)
        {
            OnVideoStarted();
        }

        private void OnVideoLoopPointReached(VideoPlayer source)
        {
            OnVideoLoopPointReached();
        }

        public override void ChangeMediaPlaySpeed(float targetSpeed)
        {
            if (Audio != null)
            {
                Audio.pitch = targetSpeed;
            }
            if (Video != null)
            {
                if (Video.canSetPlaybackSpeed)
                {
                    Video.playbackSpeed = targetSpeed;
                }
                else
                {
                    Debug.LogError("无法改变VideoPlayer播放速度");
                }
            }
        }
    }
}
